package io.terminus.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.terminus.spzx.manager.mapper.SysRoleMapper;
import io.terminus.spzx.manager.mapper.SysRoleUserMapper;
import io.terminus.spzx.manager.service.SysRoleService;
import io.terminus.spzx.model.dto.system.SysRoleDto;
import io.terminus.spzx.model.entity.system.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 系统角色Service实现类
 * @Date 2024-05-09
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer current, Integer limit) {
        // 设置分页参数
        PageHelper.startPage(current, limit);

        // 查询满足条件的数据
        List<SysRole> sysRoleList = sysRoleMapper.findByPage(sysRoleDto);

        // 封装PageInfo对象
        return new PageInfo<>(sysRoleList);
    }

    @Override
    public Map<String, Object> findByUserId(Long userId) {
        Map<String, Object> roleMap = new HashMap<>();
        // 1、查询出所有的角色列表
        List<SysRole> allRoleList = sysRoleMapper.findAll();
        roleMap.put("allRoleList", allRoleList);

        // 2、查询出分配过的角色列表
        List<Long> assignedRoleList = sysRoleUserMapper.findByUserId(userId);
        roleMap.put("assignedRoleList", assignedRoleList);

        return roleMap;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
    }

    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteById(roleId);
    }
}
